package com.example.demo.service;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.pojo.Order;
import org.mengyun.tcctransaction.api.Compensable;
import org.mengyun.tcctransaction.api.TransactionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@Service
public class BookService {

    @Resource
    RestTemplate restTemplate;

    public void cancelBook(Order order){
        // 这里就没写了，根据业务进行还原
    }

   public void bugBook(Order order){

       HttpHeaders headers = new HttpHeaders();
       headers.setContentType(MediaType.APPLICATION_JSON);
//       HttpEntity<Object> httpEntity = new HttpEntity<Object>(JSONObject.toJSON(order), headers);
       HttpEntity<Object> httpEntity = new HttpEntity<Object>("", headers);
       //System.out.println(httpEntity.toString());
       String url = "http://localhost:8081/book/bug_book/"+order.getBookId()+"/"+order.getNum();
       ResponseEntity<String> res = restTemplate.postForEntity(url, httpEntity, String.class);
       //System.out.println(res);
       if (!res.getStatusCode().is2xxSuccessful()) {
//           return "ok";
           throw new RuntimeException("请求reduceStock 错误");
       }
   }
}
